package day04.area;

import java.util.ArrayList;
import java.util.Scanner;

public class ShapeAreaCalculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<Shape> shapes = new ArrayList<>();

        System.out.println("欢迎使用图形面积计算器！");
        System.out.println("请输入操作编号：");
        System.out.println("1 - 添加圆形");
        System.out.println("2 - 添加矩形");
        System.out.println("3 - 计算所有图形面积");
        System.out.println("4 - 退出程序");

        int choice = scanner.nextInt();
        while (choice != 4) {
            switch (choice) {
                case 1:
                    System.out.print("请输入圆形的半径：");
                    double radius = scanner.nextDouble();
                    if (radius > 0) {
                        shapes.add(new Circle(radius));
                        System.out.println("圆形添加成功！");
                    } else {
                        System.out.println("半径必须大于0，请重新尝试！");
                    }
                    break;
                case 2:
                    System.out.print("请输入矩形的长：");
                    double length = scanner.nextDouble();
                    System.out.print("请输入矩形的宽：");
                    double width = scanner.nextDouble();
                    if (length > 0 && width > 0) {
                        shapes.add(new Rectangle(length, width));
                        System.out.println("矩形添加成功！");
                    } else {
                        System.out.println("长和宽必须大于0，请重新尝试！");
                    }
                    break;
                case 3:
                    if (shapes.isEmpty()) {
                        System.out.println("当前没有添加任何图形！");
                    } else {
                        System.out.println("所有图形的面积如下：");
                        for (int i = 0; i < shapes.size(); i++) {
                            Shape shape = shapes.get(i);
                            System.out.printf("图形 %d 的面积：%.2f\n", i + 1, shape.calculateArea());
                        }
                    }
                    break;
                default:
                    System.out.println("无效的操作编号，请重新输入！");
            }

            System.out.println("请输入操作编号：");
            choice = scanner.nextInt();
        }

        System.out.println("感谢使用图形面积计算器，再见！");
        scanner.close();
    }
}